package org.nado.web;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.nado.member.MemberService;
import org.nado.member.MemberVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller("memberController")
@RequestMapping(value = "/member/*")
@SessionAttributes({ "randomStr", "loginInfo", "chkId","MemberVO", "chk_mem_id" })
public class MemberController {

	private static final Logger logger = LoggerFactory
			.getLogger(MemberController.class);

	@Inject
	MemberService service;

	@RequestMapping(value = "/")
	public String getIndex() {

		logger.info("getIndex.........");

		return "member/index";

	}

	@RequestMapping(value = "/regist")
	public String regist(/* MemberVO vo */) throws Exception {

		return "member/registerPage";

	}

	
	@RequestMapping(value = "/get")
	public String get(@RequestParam("mem_id") String mem_id, Model model)
			throws Exception {
		
		logger.info("get........" + mem_id);
		String id = mem_id;
		
		System.out.println(service);
		
		MemberVO vo = service.get(id);
		
		logger.info("vo....." + vo);
		
		model.addAttribute("MemberVO", vo);
		
		return "member/memberInfo";
	}

	
	@RequestMapping(value = "/update")
	public String update(MemberVO vo) throws Exception {
		logger.info("update........." + vo);

		return "member/modifyMember";
	}

	@RequestMapping(value = "/modify")
	public String modify(MemberVO vo, Model model) throws Exception {
		logger.info("modify........." + vo);
		
		service.modify(vo);
		
		model.addAttribute("MemberVO", vo);
		return "member/memberInfo";
	}

	@RequestMapping(value = "/remove")
	public String remove(@RequestParam("mem_id") String mem_id) throws Exception {
		logger.info("remove.........." + mem_id);

		service.remove(mem_id);

		return "member/removeSuccess";

	}

	@RequestMapping(value = "/repeatedchk")
	public String checkID(String mem_id) throws Exception {

		return "member/repeatedchk";
	}

	@RequestMapping(value = "/member/chkregist", method = RequestMethod.POST)
	public String checkregisrandomStrt(MemberVO vo, String check, Model model,
			@ModelAttribute("randomStr") String random) throws Exception {

		logger.info("check..........." + check);
		logger.info("randomStr.........." + random);
		logger.info("check........." + vo);
		
		model.addAttribute("MemberVO", vo);
		
		if (!check.equals(random)) {
			return "member/registFail";
		}
		service.regist(vo);

		return "member/registSuccess";
	}

	@RequestMapping(value = "/login")
	public String login() throws Exception {
		logger.info("login...........................");
		return "member/login";
	}
	@RequestMapping(value = "/loginbox")
	public String loginbox() throws Exception {
		logger.info("login...........................");
		return "member/loginbox";
	}

	@RequestMapping(value = "chklogin", method = RequestMethod.POST)
	public String loginChk(MemberVO vo, Model model) throws Exception {

		logger.info("vo........." + vo);

		MemberVO login = service.login(vo.getMem_id(), vo.getMem_pw());

		logger.info("login.........." + login);
		if (login != null) {
			model.addAttribute("loginInfo", login);
			return "main";
		} else {
			return "member/login";
		}
	}
	@RequestMapping(value = "chkloginbox", method = RequestMethod.POST)
	public String loginboxChk(MemberVO vo, Model model) throws Exception {
		
		logger.info("vo........." + vo);
		
		MemberVO login = service.login(vo.getMem_id(), vo.getMem_pw());
		
		logger.info("login.........." + login);
		if (login != null) {
			model.addAttribute("loginInfo", login);
			return "member/loginbox";
		} else {
			return "member/loginbox";
		}
	}

	@RequestMapping(value = "chkid")
	public String chkId(MemberVO vo, Model model) throws Exception {

		logger.info("chkid........." + vo);
		model.addAttribute("MemberVO", vo);
		

		 vo = service.get(vo.getMem_id());

		if (vo == null) {
			model.addAttribute("chkId", false);
		} else {
			model.addAttribute("chkId", true);
		}
		
		return "member/registerPage";
	}

	@RequestMapping(value = "logout")
	public String logout() {

		logger.info("logout.........");	

		return "main";

	}

}
